package com.linxiao.hrcrm.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException;
import com.linxiao.hrcrm.domain.CrmCustomer;
import com.linxiao.hrcrm.service.ICrmCustomerService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmBalanceFlowMapper;
import com.linxiao.hrcrm.domain.CrmBalanceFlow;
import com.linxiao.hrcrm.service.ICrmBalanceFlowService;

import javax.annotation.Resource;

/**
 * 余额流水记录Service业务层处理
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@Service
public class CrmBalanceFlowServiceImpl implements ICrmBalanceFlowService {
    @Resource
    private CrmBalanceFlowMapper crmBalanceFlowMapper;
    @Resource
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询余额流水记录
     *
     * @param id 余额流水记录主键
     * @return 余额流水记录
     */
    @Override
    public CrmBalanceFlow selectCrmBalanceFlowById(Long id) {
        return crmBalanceFlowMapper.selectCrmBalanceFlowById(id);
    }

    /**
     * 查询余额流水记录列表
     *
     * @param crmBalanceFlow 余额流水记录
     * @return 余额流水记录
     */
    @Override
    public List<CrmBalanceFlow> selectCrmBalanceFlowList(CrmBalanceFlow crmBalanceFlow) {
        List<CrmBalanceFlow> crmBalanceFlows = crmBalanceFlowMapper.selectCrmBalanceFlowList(crmBalanceFlow);
        if (CollUtil.isNotEmpty(crmBalanceFlows)) {
            crmBalanceFlows.parallelStream().forEach(cbf -> {
                CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(cbf.getCustomerId());
                if (crmCustomer != null) {
                    cbf.setCustomerName(crmCustomer.getCustomerName());
                } else {
                    cbf.setCustomerName("未知");
                }
            });
        }
        return crmBalanceFlows;
    }

    /**
     * 新增余额流水记录
     *
     * @param crmBalanceFlow 余额流水记录
     * @return 结果
     */
    @Override
    public int insertCrmBalanceFlow(CrmBalanceFlow crmBalanceFlow) {
        crmBalanceFlow.setCreateTime(DateUtils.getNowDate());
        return crmBalanceFlowMapper.insertCrmBalanceFlow(crmBalanceFlow);
    }

    /**
     * 修改余额流水记录
     *
     * @param crmBalanceFlow 余额流水记录
     * @return 结果
     */
    @Override
    public int updateCrmBalanceFlow(CrmBalanceFlow crmBalanceFlow) {
        crmBalanceFlow.setUpdateTime(DateUtils.getNowDate());
        return crmBalanceFlowMapper.updateCrmBalanceFlow(crmBalanceFlow);
    }

    /**
     * 批量删除余额流水记录
     *
     * @param ids 需要删除的余额流水记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmBalanceFlowByIds(Long[] ids) {
        return crmBalanceFlowMapper.deleteCrmBalanceFlowByIds(ids);
    }

    /**
     * 删除余额流水记录信息
     *
     * @param id 余额流水记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmBalanceFlowById(Long id) {
        return crmBalanceFlowMapper.deleteCrmBalanceFlowById(id);
    }

    /**
     * 我的余额流水
     * @param crmBalanceFlow
     * @return
     */
    @Override
    public List<CrmBalanceFlow> findByUser(CrmBalanceFlow crmBalanceFlow) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CrmCustomer customer = crmCustomerService.selectCrmCustomerByMemberId(crmBalanceFlow.getMchId(), loginUser.getUserId());
        if (customer == null) {
            throw new UtilException("客户信息有误");
        }
        crmBalanceFlow.setCustomerId(customer.getId());

        List<CrmBalanceFlow> crmBalanceFlows = crmBalanceFlowMapper.selectCrmBalanceFlowList(crmBalanceFlow);
        if (CollUtil.isNotEmpty(crmBalanceFlows)) {
            crmBalanceFlows.parallelStream().forEach(c -> c.setCustomerName(customer.getCustomerName()));
        }
        return crmBalanceFlows;
    }
}
